/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import model.Game.Difficulties;
import model.LevelInterpreter.Parts;

/**
 *
 *
 */
public class LevelSpecificationBuilder {

    private LevelSpecification levelSpecification;
    private LevelInterpreter levelInterpreter;
    private ArrayList<String> msg;
    private final int MAXROWSCOLUMNS = 20;
    private final int MINROWSCOLUMNS = 5;

    public LevelSpecificationBuilder() {
        this.levelSpecification = new LevelSpecification();
        this.levelInterpreter = new LevelInterpreter();
        this.msg = new ArrayList<>();
    }

    public LevelSpecificationBuilder(LevelSpecification levelSpecification) {
        this.levelSpecification = levelSpecification;
        this.levelInterpreter = new LevelInterpreter();
        this.msg = new ArrayList<>();
    }

    public boolean generateFieldSpecification(int rows, int columns) {

        if (rows < MINROWSCOLUMNS || rows > MAXROWSCOLUMNS || columns < MINROWSCOLUMNS || columns > MAXROWSCOLUMNS) {
            this.getMsg().add("Het aantal rijen of kolomen moeten tussen de " + MINROWSCOLUMNS + " en de " + MAXROWSCOLUMNS + " zitten");
            return false;
        }

        int[][] temporaryFieldSpecification = new int[rows][columns];
        for (int row = 0; row < rows; row++) {
            for (int column = 0; column < columns; column++) {
                temporaryFieldSpecification[row][column] = 1;
            }
        }

        this.levelSpecification.setFieldSpecification(temporaryFieldSpecification);
        return true;
    }

    public boolean editFieldSpecification(int xFirst, int xSecond, int yFirst, int ySecond, Parts part) {

        int[][] temporaryFieldSpecification = this.getLevelSpecification().getFieldSpecification();
        int objectIdentifier = 0;
        
        switch (part) {
            case EMPTY:
                objectIdentifier = 0;
                break;
            case TERRAIN:
                objectIdentifier = 1;
                break;
            case WALL:
                objectIdentifier = 2;
                break;
            case BOX:
                objectIdentifier = 3;
                break;
            case ENEMY:
                objectIdentifier = 4;
                break;
            case PLAYER:
                objectIdentifier = 5;
                break;
        }



        if (xSecond < xFirst) {
            xSecond = xFirst;
        }

        if (ySecond < yFirst) {
            ySecond = yFirst;
        }

        int xLimit = this.levelSpecification.getFieldSpecification().length;
        int yLimit = this.levelSpecification.getFieldSpecification()[0].length;

        if (xFirst > xLimit || xSecond > xLimit || yFirst > yLimit || xSecond > yLimit) {
            this.getMsg().add("Maak X en Y niet groter of kleiner dan het speelveld");
            return false;
        }

        for (int y = yFirst; y <= ySecond; y++) {
            for (int x = xFirst; x <= xSecond; x++) {
                temporaryFieldSpecification[y][x] = objectIdentifier;
            }
        }

        this.getLevelSpecification().setFieldSpecification(temporaryFieldSpecification);
        return true;
    }

    /**
     * @return the levelSpecification
     */
    public LevelSpecification getLevelSpecification() {
        return levelSpecification;
    }

    /**
     * @return the levelInterpreter
     */
    public LevelInterpreter getLevelInterpreter() {
        return levelInterpreter;
    }

    public boolean setLevelSpecificationImage(Parts part, BufferedImage image) {
        if (!this.levelSpecification.setImage(part, image)) {
            this.getMsg().addAll(this.levelSpecification.getMsg());
            this.levelSpecification.clearMsg();
            return false;
        }
        return true;
    }
    
    public void setLevelSpecificationDifficulty(Difficulties difficulty){
        this.levelSpecification.setDifficulty(difficulty);
    }
    
    /**
     * @return the msg
     */
    public ArrayList<String> getMsg() {
        return msg;
    }
    
    public void clearMsg() {
        msg = new ArrayList<>();
    }
}
